﻿using App.Configuration;
using System.Collections.Generic;
using System.Linq;

namespace App.EntityFramework.Seed
{
    internal class DefaultMenuPermissionsCreator
    {
        private readonly WebPlusDbContext _context;

        public DefaultMenuPermissionsCreator(WebPlusDbContext _context)
        {
            this._context = _context;
        }

        internal void Create()
        {
            var menus = new List<MenuPermission> {
                new MenuPermission {
                    MenuText = "系统",
                    MenuLink= "#",
                    SortOrder = 1,
                    MenuType = "menu",
                    IsActivated = "on",
                    AutoCreated = false,
                    ChildMenuPermissions = new List<MenuPermission> {
                        new MenuPermission {
                            MenuText = "租户管理",
                            MenuLink= "system/tenants",
                            SortOrder = 1,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            IsHostTenant = true,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "新增",
                                    MenuLink= "insert",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "撤销",
                                    MenuLink= "undo",
                                    SortOrder = 2,
                                    IconCls="fa-undo",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "保存",
                                    MenuLink= "save",
                                    SortOrder = 3,
                                    IconCls="fa-save",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "系统设置",
                            MenuLink= "system/settings",
                            SortOrder = 1,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "新增",
                                    MenuLink= "insert",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "撤销",
                                    MenuLink= "undo",
                                    SortOrder = 2,
                                    IconCls="fa-undo",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "保存",
                                    MenuLink= "save",
                                    SortOrder = 3,
                                    IconCls="fa-save",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "功能菜单",
                            MenuLink= "system/MenuPermissions",
                            SortOrder = 2,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            IsHostTenant = true,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "添加",
                                    MenuLink= "add",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "修改",
                                    MenuLink= "edit",
                                    SortOrder = 2,
                                    IconCls="fa-edit",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "按钮管理",
                            MenuLink= "system/buttons",
                            SortOrder = 3,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            IsHostTenant = true,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "新增",
                                    MenuLink= "insert",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "撤销",
                                    MenuLink= "undo",
                                    SortOrder = 2,
                                    IconCls="fa-undo",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "保存",
                                    MenuLink= "save",
                                    SortOrder = 3,
                                    IconCls="fa-save",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "角色管理",
                            MenuLink= "user/roles",
                            SortOrder = 4,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "新增",
                                    MenuLink= "insert",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "撤销",
                                    MenuLink= "undo",
                                    SortOrder = 2,
                                    IconCls="fa-undo",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "保存",
                                    MenuLink= "save",
                                    SortOrder = 3,
                                    IconCls="fa-save",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "代码生成",
                            MenuLink= "system/generation",
                            SortOrder = 5,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            IsHostTenant = true
                        },
                        new MenuPermission {
                            MenuText = "文件管理",
                            MenuLink= "system/filemanage",
                            SortOrder = 6,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            IsHostTenant = true
                        },
                        new MenuPermission {
                            MenuText = "字典管理",
                            MenuLink= "system/dictionarys",
                            SortOrder = 7,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            IsHostTenant = true,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "新增",
                                    MenuLink= "insert",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "撤销",
                                    MenuLink= "undo",
                                    SortOrder = 2,
                                    IconCls="fa-undo",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "保存",
                                    MenuLink= "save",
                                    SortOrder = 3,
                                    IconCls="fa-save",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true,
                                    IsHostTenant = true
                                }
                            }
                        }
                    }
                },
                new MenuPermission {
                    MenuText = "用户",
                    MenuLink= "#",
                    SortOrder = 1,
                    MenuType = "menu",
                    IsActivated = "on",
                    AutoCreated = false,
                    ChildMenuPermissions = new List<MenuPermission> {
                        new MenuPermission {
                            MenuText = "部门管理",
                            MenuLink= "user/teams",
                            SortOrder = 1,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "添加",
                                    MenuLink= "add",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "修改",
                                    MenuLink= "edit",
                                    SortOrder = 2,
                                    IconCls="fa-edit",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "用户管理",
                            MenuLink= "user/users",
                            SortOrder = 2,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "添加",
                                    MenuLink= "add",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "修改",
                                    MenuLink= "edit",
                                    SortOrder = 2,
                                    IconCls="fa-edit",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                }
                            }
                        }
                    }
                },
                new MenuPermission {
                    MenuText = "商品",
                    MenuLink= "#",
                    SortOrder = 1,
                    MenuType = "menu",
                    IsActivated = "on",
                    AutoCreated = false,
                    ChildMenuPermissions = new List<MenuPermission> {
                        new MenuPermission {
                            MenuText = "品牌管理",
                            MenuLink= "goods/brands",
                            SortOrder = 1,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "新增",
                                    MenuLink= "insert",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "撤销",
                                    MenuLink= "undo",
                                    SortOrder = 2,
                                    IconCls="fa-undo",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "保存",
                                    MenuLink= "save",
                                    SortOrder = 3,
                                    IconCls="fa-save",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "分类管理",
                            MenuLink= "goods/categorys",
                            SortOrder = 2,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "添加",
                                    MenuLink= "add",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "修改",
                                    MenuLink= "edit",
                                    SortOrder = 2,
                                    IconCls="fa-edit",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                }
                            }
                        },
                        new MenuPermission {
                            MenuText = "商品管理",
                            MenuLink= "goods/index",
                            SortOrder = 3,
                            MenuType = "menu",
                            IsActivated = "on",
                            AutoCreated = false,
                            ChildMenuPermissions = new List<MenuPermission> {
                                new MenuPermission {
                                    MenuText = "添加",
                                    MenuLink= "add",
                                    SortOrder = 1,
                                    IconCls="fa-plus",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "修改",
                                    MenuLink= "edit",
                                    SortOrder = 2,
                                    IconCls="fa-edit",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                },
                                new MenuPermission {
                                    MenuText = "删除",
                                    MenuLink= "del",
                                    SortOrder = 4,
                                    IconCls="fa-remove",
                                    MenuType = "button",
                                    IsActivated = "on",
                                    AutoCreated = true
                                }
                            }
                        }
                    }
                }
            };

            foreach (var record in menus)
            {
                if (_context.MenuPermission.Any(s => s.MenuText == record.MenuText))
                {
                    break;
                }

                var childRecords = record.ChildMenuPermissions.ToList();

                record.ChildMenuPermissions.Clear();

                _context.MenuPermission.Add(record);
                _context.SaveChanges();

                foreach (var childRecord in childRecords)
                {
                    var buttonRecords = new List<MenuPermission>();
                    if (childRecord.ChildMenuPermissions != null) {
                        buttonRecords = childRecord.ChildMenuPermissions.ToList();
                        childRecord.ChildMenuPermissions.Clear();
                    }
                    
                    childRecord.ParentId = record.Id;

                    _context.MenuPermission.Add(childRecord);
                    _context.SaveChanges();

                    foreach (var buttonRecord in buttonRecords)
                    {
                        buttonRecord.ParentId = childRecord.Id;

                        _context.MenuPermission.Add(buttonRecord);
                        _context.SaveChanges();
                    }
                }
            }
        }
    }
}